package com.tarena.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.tarena.dao.MeettingaddressDao;
import com.tarena.entity.Meettingaddress;
import com.tarena.entity.Meettingroom;
import com.tarena.service.MeettingaddressService;

@Service
public class MeettingaddressServiceImpl implements MeettingaddressService {

	@Resource
	MeettingaddressDao mas;

	public boolean save(Meettingaddress meettingaddress) {
		Integer sum = mas.getSum();
		mas.save(meettingaddress);
		if (sum < mas.getSum()) {
			return true;
		}
		return false;
	}

	public boolean delete(Integer id) {
		Integer sum = mas.getSum();
		System.out.println("id" + id);
		List<Meettingroom> list = mas.findByAddressId(id);
		System.out.println("aaa" + list.size());
		if (null != list && 0 != list.size()) {
			return false;
		} else {
			mas.delete(id);
			if (sum > mas.getSum()) {
				return true;
			}
			return false;
		}

	}

	public void update(Meettingaddress meettingaddress) {
		mas.update(meettingaddress);

	}

	public List<Meettingaddress> findById(Integer id) {
		return mas.findById(id);
	}

	public List<Meettingaddress> findAllMeettingaddress() {
		return mas.findAllMeettingaddress();
	}

	public Integer getSum() {
		return mas.getSum();
	}

	public List<Meettingaddress> findByPage(int offset, int limit,
			Meettingaddress meettingaddress) throws Exception {
		RowBounds rb = new RowBounds(offset, limit);
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		SqlSessionFactory sf = (SqlSessionFactory) ctx
				.getBean(SqlSessionFactory.class);
		SqlSession session = sf.openSession();
		List<Meettingaddress> list = session.selectList(
				"findMeettingaddressByParam", meettingaddress, rb);
		session.close();
		return list;
	}

	public List<Meettingaddress> findByPage(int offset, int limit)
			throws Exception {
		RowBounds rb = new RowBounds(offset, limit);
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		SqlSessionFactory sf = (SqlSessionFactory) ctx
				.getBean(SqlSessionFactory.class);
		SqlSession session = sf.openSession();
		List<Meettingaddress> list = session.selectList("findMeettingaddress",
				null, rb);
		session.close();
		return list;
	}

	public List<Meettingroom> findByAddressId(Integer meettingAddressid) {
		return mas.findByAddressId(meettingAddressid);
	}

	public List<Meettingaddress> findEmp(Meettingaddress meettingaddress) {
		return mas.findEmp(meettingaddress);
	}

	public Meettingaddress findMeettingAddressAndMeettingRoom(int id) {
		return mas.findMeettingAddressAndMeettingRoom(id);
	}

}
